package com.heiyu.controller;

import com.alibaba.fastjson.JSON;
import com.heiyu.entity.UserInfo;
import com.heiyu.utils.CacheKey;
import com.heiyu.utils.CoinBaseInfoUtils;
import com.heiyu.utils.MarkInfoUtils;
import com.heiyu.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by mountain on 18/6/11.
 */
public class ApiBaseController {

    protected String AUTH_KEY = "Authorazition";
    @Autowired
    protected RedisUtils redisUtils;
    @Autowired
    protected CoinBaseInfoUtils coinBaseInfoUtils;
    @Autowired
    protected MarkInfoUtils markInfoUtils;

    protected String defaultSymbol="CNY";


    protected String getSubUrl(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String subUrl = request.getRequestURL().toString();
                return subUrl;
    }

    protected  String getAddress(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getRemoteAddr();
    }

    protected String getNickName(UserInfo userInfo){
        if(StringUtils.isEmpty(userInfo.getNickName())){
            return userInfo.getTelphone().substring(0,4)+"****"+userInfo.getTelphone().substring(8);
        }
        return userInfo.getNickName();
    }

    public UserInfo getUser(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("Authorization");
        if(StringUtils.isEmpty(token)){
            return null;
        }
        UserInfo userInfo = JSON.parseObject(redisUtils.get(CacheKey.USER_INFO +token),UserInfo.class);
        return userInfo;
    }

    public boolean defaultSymbol(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String symbol = request.getHeader("defaultSymbol");
        if(StringUtils.isEmpty(symbol)){
            return true;
        }
        return symbol.equals(defaultSymbol);
    }

    protected void setHeader(String key,String value){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.addHeader(key,value);
    }

    protected  boolean isMobile(final String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,5,7,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }
}
